package com.aifast.dict.core;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.aifast.dict.cache.ICacheService;
import com.aifast.dict.common.Const;
import com.aifast.dict.properties.FastDictProperties;
import com.jfinal.plugin.activerecord.Db;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author AiFast
 * @since 2022/12/28 15:27
 */
@Component
public class FastDictService implements IFastDictService{

    @Autowired
    private FastDictProperties properties;
    @Autowired
    private ICacheService iCacheService;

    @Override
    public String getDictDataValue(String type, Object fieldValue) {
        if (StrUtil.isBlank(type) || ObjectUtil.isNull(fieldValue)) {
            return Const.EMPTY;
        }
        String strKey = getDictStrKey(type, fieldValue);
        String cacheValue = iCacheService.get(strKey);
        if(StrUtil.isNotBlank(cacheValue)){
            return cacheValue;
        }

        String sql = "select {} from {} where {} = '{}' and {} = {} limit 1";
        String formatSql = StrUtil.format(sql, properties.getSource().getName()
                , properties.getSource().getTableName()
                , properties.getSource().getType(), type
                , properties.getSource().getValue(), fieldValue);
        String queryStr = Db.queryStr(formatSql);
        if(StrUtil.isNotBlank(queryStr)){
            iCacheService.set(strKey,queryStr);
        }
        return queryStr;
    }

    @Override
    public String getTableDataValue(String tableName, String columns,String idColumns, Object dataId) {
        if(StrUtil.isBlank(tableName) || StrUtil.isBlank(columns) || ObjectUtil.isNull(dataId)){
            return Const.EMPTY;
        }

        String tableKey = getDictTableKey(tableName,columns,dataId);
        String cacheValue = iCacheService.get(tableKey);
        if(StrUtil.isNotBlank(cacheValue)){
            return cacheValue;
        }

        String sql = "select {} from {} where {} = {}";
        String formatSql = StrUtil.format(sql, columns, tableName,idColumns, dataId);

        String queryStr = Db.queryStr(formatSql);
        if(StrUtil.isNotBlank(queryStr)){
            iCacheService.set(tableKey,queryStr);
        }
        return queryStr;
    }


    @Override
    public void evict(String type, Object fieldValue) {
        String strKey = getDictStrKey(type, fieldValue);
        iCacheService.evict(strKey);
    }

    @Override
    public void evictTableDictData(String tableName, String columns, Object dataId) {
        String tableKey = getDictTableKey(tableName, columns, dataId);
        iCacheService.evict(tableKey);
    }

    /**
     * 获取字典map的键值key
     *
     * @param type 字典类型
     * @return
     */
    private static String getDictStrKey(String type,Object fieldValue) {
        return StrUtil.join(":", Const.PROJECT_NAME_UC,"dict", "str", type,fieldValue);
    }

    private static String getDictTableKey(String tableName, String columns, Object dataId) {
        return StrUtil.join(":", Const.PROJECT_NAME_UC,"dict", "table", tableName,columns,dataId);
    }

}
